
package com.phevos.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyDigest2
{
    private static final Logger logger = LoggerFactory.getLogger(MyDigest2.class.getName());

    private MyDigest2()
    {
    }

    /**
     * ���ַ�ת���ɰ���
     * 
     * @param info String ԭʼ�ַ�
     * @return String ת��֮����ַ�
     */
    public static String encrypto(String info)
    {
        String cipherByte = new BASE64Encoder().encode(info.getBytes());
        return cipherByte;
    }

    /**
     * ��ԭ����encrypto����ת���İ���
     * 
     * @param chiperByte String ����
     * @return String ԭ��
     */
    public static String decrypto(String chiperByte)
    {
        String info = new String(new BASE64Decoder().decodeBuffer(chiperByte));
        return info;
    }

    /**
     * <p>
     * ��ɼ����ļ�
     * </p>
     */
    private static void generator()
    {
        System.out.println("========================================");
        System.out.println("======= MyDigest verision 1.0.0 ========");
        System.out.println("========================================");
        System.out.println("please input args:[arg_1];[arg_2];...;[arg_n]");
        System.out.println("eg: test1;test2");
        try
        {
            String[] infos = readInput(System.in, ";");
            if (infos.length < 1)
            {
                System.exit(-1);
            }
            for (int i = 0; i < infos.length; i++)
            {
                String res = encrypto(infos[i]);
                System.out.println(infos[i] + " ====>" + res);
            }

        }
        catch (Exception e)
        {
            logger.error("",e);
            System.exit(-1);
        }
    }

    /**
     * <p>
     * ��InputStrem�ֽ�ΪString[]
     * </p>
     * 
     * @param in
     * @return
     */
    private static String[] readInput(InputStream in, String split) throws IOException
    {
        String[] result = null;
        BufferedReader bufferReader = null;
        if (in == null || split == null)
            return result;
        bufferReader = new BufferedReader(new InputStreamReader(in));
        String cmd = bufferReader.readLine();
        result = StringUtil.splitStr(cmd, split);
        bufferReader.close();
        return result;
    }

    public static void main(String[] args)
    {
        if (args == null || args[0] == null)
        {
            System.out.println("args can't be null.");
        }
        else
        {
            String cmd = args[0].toUpperCase();
            if (cmd.equals("G"))
            {
                MyDigest2.generator();
            }
            else if (cmd.equals("D"))
            {
                if (args.length >= 2 || args[1] != null)
                {
                    System.out.print(decrypto(args[1]));
                }
            }
        }
    }
}
